'''
Created on 15/10/2011

@author: Digao
'''


def toInt(arg):
    return int(arg)

def toStr(a):
    return str(a)

def change(nlist,to_change):
    ch_number = int(nlist[to_change])
    for i in xrange(-1,to_change,-1):
        if nlist[i]>ch_number:
            nlist[to_change]=nlist[i]
            nlist[i]=ch_number
            rest = nlist[to_change+1:]
            nlist = nlist[:to_change+1]
            rest.sort()
            nlist.extend(rest)
            return nlist
    

def next_number(nlist,after_zero=False):
    for i in xrange(-1,-len(nlist),-1):
        if nlist[i]>nlist[i-1]:
            to_change=i-1
            nlist = change(nlist,to_change)
            return ''.join(map(toStr,nlist))
    nlist.append(0)
    nlist.sort()
    index=0
    for i in xrange(len(nlist)):
        if nlist[i]>0:
            index=i
            break
    tmp=nlist[index]
    nlist[index]=nlist[0]
    nlist[0]=tmp
    return ''.join(map(toStr,nlist))
    
    


input = open('input.in')
CASES = int(input.readline())
for case in range(1,CASES+1):
    N = input.readline().strip()
    nlist=[int(d) for d in N ]
    result = int(next_number(nlist))
    print 'Case #%d: %d'%(case,result)
    
    

